package com.wanda.controller;

import java.io.File;
import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.wanda.model.BuildModel;
import com.wanda.model.BuildProModel;
import com.wanda.model.JsonModelGeneral;
import com.wanda.model.ProjectModel;
import com.wanda.model.RoomModel;
import com.wanda.model.RoomPBUAPModel;
import com.wanda.model.UnitModel;
import com.wanda.model.UnitPBModel;
import com.wanda.model.User_Role_OrganizationModel;
import com.wanda.model.Wd_UserModel;
import com.wanda.service.BuildService;
import com.wanda.service.ProjectService;
import com.wanda.service.RoomService;
import com.wanda.service.UnitService;
import com.wanda.service.Wd_UserService;
import com.wanda.upload.ExcelOperate;
import com.wanda.upload.ExportRoom;
import com.wanda.util.DateFormat;



@Controller
@RequestMapping("qt/ht/room")
public class RoomController {

	@Autowired
	private RoomService roomService;
	@Autowired
	private Wd_UserService wd_UserService;
	@Autowired
	private ProjectService projectService;
	@Autowired
	private BuildService buildService;
	@Autowired
	private UnitService unitService;
	// 添加房间
	@RequestMapping(value = "/addRoom.do")
	public @ResponseBody Map<String, Object> add(HttpServletRequest request ,RoomModel model) {

		System.out.println("进入添加");

		Wd_UserModel user = (Wd_UserModel) request.getSession().getAttribute("bean");
		RoomModel models = new RoomModel();

		models.setBuild_up_area(model.getBuild_up_area());
		models.setRoom_code(model.getRoom_code());
		models.setApartment_layout_id(model.getApartment_layout_id());
		models.setInner_area(model.getInner_area());
		models.setUnit_price(model.getUnit_price());
		models.setFloor(model.getFloor());
		models.setState(3);
		models.setUnit_id(model.getUnit_id());
		models.setProject_id(model.getProject_id());
		models.setPurpose_id(model.getPurpose_id());
		models.setBuild_id(model.getBuild_id());
		models.setOpening_report_id(model.getOpening_report_id());
		models.setBuild_up_area(model.getBuild_up_area());
		models.setInner_price(model.getInner_price());
		models.setStandard_price(model.getStandard_price());
		models.setInners(model.getInners());
		models.setUnits(model.getUnits());
		models.setCreate_time(DateFormat.getNowDate());
        models.setTel(model.getTel());
		
		if (!("admin".equals(user.getTel()))) {
	    	model.setTel(user.getTel());
	    	 System.err.println("tel11111111:"+user.getTel());
	    	 roomService.insertRoom(models);
	    	Map<String, Object> map = new HashMap<String, Object>();
			System.out.println("添加成功");
			map.put("msg", "成功");

			return map;
	    }else{
	    	model.setTel(models.getTel());
	    	 System.err.println("tel2222222222:"+models.getTel());
	    	 roomService.insertRoom(models);
	    	Map<String, Object> map = new HashMap<String, Object>();
			System.out.println("添加成功");
			map.put("msg", "成功");

			return map;
	    }
	}

	//判断登录人
	@RequestMapping(value = "/selectAllByAdd.do")
	public ModelAndView showAddAll(HttpServletRequest request) {
		ModelAndView model = new ModelAndView();
		
		List<BuildProModel> listpp = null;
		List<BuildModel> listbu= new ArrayList<BuildModel>();
		List<ProjectModel> listp = null;
		List<ProjectModel> listpjm= new ArrayList<ProjectModel>();
		List<UnitPBModel> listppp = null;
		List<UnitPBModel> listun= new ArrayList<UnitPBModel>();
		
		List<User_Role_OrganizationModel> abc=null;
		try{
			 listp = projectService.selectAll();
		     listpp = buildService.selectAll();
		     listppp=unitService.selectAll();
		     Wd_UserModel user = (Wd_UserModel) request.getSession().getAttribute("bean");
			if (!(user.getTel().equals("admin")||user.getTel()==("admin"))) {
				for (ProjectModel p : listp) {
					if(p.getTel()!=null){
						if(p.getTel().equals(user.getTel())){
							listpjm.add(p);
							System.out.println("进入项目查询"+p.getTel());
							System.out.println("进入项目查询"+p.getProject_name());
						}	
					}
				}	
				for(BuildModel pp: listpp){
					if(pp.getTel()!=null){
						if(pp.getTel().equals(user.getTel())){
							listbu.add(pp);
							System.out.println("进入楼栋查询"+pp.getTel());
							System.out.println("进入楼栋查询"+pp.getBuild_name());
						}	
					}
			    }
				for(UnitPBModel pp: listppp){
					if(pp.getTel()!=null){
						if(pp.getTel().equals(user.getTel())){
							listun.add(pp);
							System.out.println("进入单元查询"+pp.getTel());
							System.out.println("进入单元查询"+pp.getUnit_code());
						}	
					}
			    }
			}
			if (!(user.getTel().equals("admin")||user.getTel()==("admin"))) {
			    System.err.println("wo bu shi admin1:" +listpjm.size());
			    System.err.println("wo bu shi admin1:" +listbu.size());
			    model.addObject("proj", listpjm);
			    model.addObject("bu",listbu);
			    model.addObject("un",listun);
			}else{
				System.err.println("wo  shi admin2:"+listp);
				System.err.println("wo  shi admin2:"+listpp);
				model.addObject("proj", listp);
				model.addObject("bu",listpp);
				model.addObject("un",listppp);
			}
			abc = wd_UserService.selectuserAll();
			System.err.println("listppp:"+abc);
		}catch (Exception ex) {
			ex.printStackTrace();
		}
		model.setViewName("/ht/addRoom.jsp");
		model.addObject("rooms", abc);
		return model;
		
	}
	
	// 修改房间
	@RequestMapping(value = "/updateRoom.do")
	
	
	
	public String update(RoomModel model) {

		System.out.println("进入修改房间");

		model.setUpdate_time(DateFormat.getNowDate());

		System.out.println("修改房间的名字:::::::" + model.getRoom_code());

		roomService.updateRoom(model);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("room", map);
		return "redirect:/qt/ht/room/selectAll.do";
	}

	// 删除房间
	@RequestMapping(value = "/deleteRoom.do")
	public String delete(RoomModel model) {

		System.out.println("进入删除房间");
		model.setRoom_id(model.getRoom_id());
		model.setDelete_time(DateFormat.getNowDate());
		int room = roomService.deleteRoom(model);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("msg", room);

		return "redirect:/qt/ht/room/selectAll.do";
	}

	// 查询单条记录
	@RequestMapping(value = "/selectById.do")
	public ModelAndView selectById(String id) {

		System.out.println("进入单条查询");
		System.out.println("id:" + id);
		ModelAndView model = new ModelAndView();
		RoomModel room = null;
		try {

			room = roomService.selectById(id);
			model.setViewName("/ht/updateRoom.jsp");
			model.addObject("rooms", room);
			// System.out.println("1111111");

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return model;
	}

	// 查询房间所有信息
	@RequestMapping(value = "/selectAll.do")
	public ModelAndView showAll() {

		System.out.println("进入房间查询");

		ModelAndView model1 = new ModelAndView();

		List<RoomPBUAPModel> rooms = null;
		try {

			rooms = roomService.selectAll();
			model1.setViewName("/ht/listRoom.jsp");
			model1.addObject("rooms", rooms);

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return model1;
	}

	// 查询房间所有信息
	@RequestMapping(value = "/selectRooms.do")
	public ModelAndView showAllss() {

		System.out.println("进入房间查询");

		ModelAndView model1 = new ModelAndView();

		List<RoomPBUAPModel> rooms = null;
		try {

			rooms = roomService.selectAll();
			model1.setViewName("/qt/rooms.jsp");
			model1.addObject("rooms", rooms);

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return model1;
	}
	
	// 查询房间所有信息
	@RequestMapping(value = "/selectAlls.do")
	public ModelAndView showAlls(@Param(value = "project_id") String project_id, HttpServletRequest request) {

		ModelAndView model1 = new ModelAndView();
		request.getSession().setAttribute("namea", project_id);
		System.out.println("123123123:"+project_id);
		RoomPBUAPModel mod = new RoomPBUAPModel();
		mod.setProject_id(project_id);
		System.err.println("项目:" + project_id);
		List<RoomPBUAPModel> rooms = null;
		try {
			rooms = roomService.selectAlls(project_id);
			
			model1.setViewName("/qt/addOpeningRoom.jsp");
			model1.addObject("rooms", rooms);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return model1;
	}

	// 开盘里的房间多选
	@RequestMapping(value = "/select.do")
	public ModelAndView select(HttpServletRequest request,HttpSession session) {

		String projectid=(String) session.getAttribute("projectid");
		String openingid=(String) session.getAttribute("openingid");
		
		String[] str = request.getParameterValues("checkbox1");
		request.setAttribute("setatt", str);
		
		
		ModelAndView model1 = new ModelAndView();
		List<RoomPBUAPModel> blogs=null;
		try {
			final List ids = new ArrayList();
			if(str!=null){
			for (String st : str) {
				ids.add(st);
				System.err.println("//" + st);	
			}
			}else{
				String[] str1 =(String[]) session.getAttribute("lll");
				for (String st : str1) {
					ids.add(st);
					System.err.println("//" + st);	
				}
				
			}
			Map map = new HashMap();
			map.put("ids", ids);
			
			map.put("projectid", projectid);
			map.put("openingid", openingid);
			roomService.updates(map);

			blogs = roomService.select(map);
			System.err.println("开盘房间里选择房间的个数：" + blogs.size());
			
			session.setAttribute("ids", map);
			
			model1.setViewName("/qt/addOpeningRooms.jsp");
			model1.addObject("rooms", blogs);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return model1;
	}

	// 开盘里的房间多选
	@RequestMapping(value = "/rooms.do")
	public ModelAndView selec(HttpServletRequest request,HttpSession session) {

		String[] str = request.getParameterValues("checkbox2");

		request.setAttribute("setatt", str);
		
		ModelAndView model1 = new ModelAndView();
		List<RoomPBUAPModel> blogs=null;
		try {
			final List ids = new ArrayList();
			if(str!=null){
			for (String st : str) {
				ids.add(st);
				System.err.println("//" + st);	
			}
			}
			Map map = new HashMap();
			map.put("ids", ids);
			
			roomService.updates(map);

			blogs = roomService.select(map);
			System.err.println("开盘房间里选择房间的个数：" + blogs.size());
			
			session.setAttribute("ids", map);
			
			model1.setViewName("/qt/rooms.jsp");
			model1.addObject("room", blogs);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return model1;
	}
	
	// 查询
	@RequestMapping(value = "/selectBySql.do")
	public @ResponseBody JsonModelGeneral selectBySql(String SearchSql) {

		System.err.println("到controller啦");
		JsonModelGeneral json = new JsonModelGeneral();
		List<RoomModel> rooms = null;
		try {
			rooms = roomService.selectBySql(SearchSql);
			json.setRows(rooms);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return json;
	}

	//导出
	@RequestMapping(value = "/Export.do")
	public @ResponseBody ModelAndView export(HttpServletRequest request) {
         
		System.out.println("导出");
		String SearchSql = "";

		String[] str = request.getParameterValues("checkbox1");
		System.err.println(str);
        request.getSession().setAttribute("lll", str);
		ExportRoom indent = new ExportRoom();
		ModelAndView mav = new ModelAndView();
		mav.setViewName("indent");
		try {
			final List ids = new ArrayList();
			System.err.println("ids" + ids);
			for (String st : str) {
				ids.add(st);
				System.err.println("//" + st);
			}
			Map map = new HashMap();
			map.put("ids", ids);
			List<RoomPBUAPModel> indents = roomService.select(map);
			System.err.println("//" + indents);
			List<String> leader = new ArrayList<String>();
			leader.add("项目名称");
			leader.add("房间房号");
			leader.add("建筑面积");
			leader.add("套内面积");
			leader.add("计价方式");
			leader.add("建筑面积单价");
			leader.add("套内面积单价");
			leader.add("标准总价");
			indent.exportExcel_Code(leader, indents, "sheet1");
			JOptionPane.showMessageDialog(null, "文件已导出在d盘");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		ModelAndView mav1 = new ModelAndView();
		mav1.setViewName("select.do");
		return mav1;

	}
//导入
	@RequestMapping(value = "/updateImpl.do")
	public @ResponseBody ModelAndView ImportMyroom(HttpServletRequest request) {
		String[] str = request.getParameterValues("checkbox1");
		request.setAttribute("checkbox1",str);
		System.err.println("updateImpl.d中============================================="+str);
		try {
			ModelAndView mav = new ModelAndView();
			JFileChooser jfc = new JFileChooser("d:");
			int n = jfc.showOpenDialog(null);

			File file = jfc.getSelectedFile();
			if (file != null) {
				ExcelOperate excel = new ExcelOperate();
				String[][] result = excel.getData(file, 1);
				int rowLength = result.length;
				for (int i = 0; i < rowLength; i++) {
					// 将读取的数据存入到数据库
					RoomPBUAPModel bean = new RoomPBUAPModel();
					bean.setProject_id(result[i][0]);
					System.err.println("id:" + bean.getProject_id());
					bean.setInner_price(Float.valueOf(result[i][5]));
					System.err.println("Inner_price:" + bean.getInner_price());
					bean.setUnit_price(Float.valueOf(result[i][6]));
					System.err.println("Unit_price:" + bean.getUnit_price());
					bean.setStandard_price(Float.valueOf(result[i][7]));
					System.err.println("Unit_price:" + bean.getStandard_price());
					roomService.updateImpl(bean);
				} 
				JOptionPane.showMessageDialog(null, "导入成功");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		ModelAndView mav1 = new ModelAndView();
		mav1.setViewName("select.do");
        return mav1;
	}

}
